package org.smart4j.framework.helper;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author Jungle
 * @created 2017/5/21 20:58
 */
public class ReflectionUtil {
	private static final Logger LOGGER = LoggerFactory.getLogger(ReflectionUtil.class);

	public static Object newInstance(Class<?> cls){
		Object instance;
		try {
			instance = cls.newInstance();
		}
		catch (Exception e){
			LOGGER.error("new instance failure",e);
			throw new RuntimeException(e);
		}
		return  instance;
	}

	public static Object invokeMethod(Object obj,Method method,Object... params){
		Object result;
		try {
			method.setAccessible(true);
			result = method.invoke(obj,params);
		}
		catch (Exception e){
			LOGGER.error("invoke method failure",e);
			throw new RuntimeException(e);
		}
		return result;
	}
	public static void setField(Object obj,Field field,Object value){
		try {
			field.setAccessible(true);
			field.set(obj,value);
		}
		catch (Exception e)
		{
			LOGGER.error("set field failure",e);
			throw new RuntimeException(e);
		}
	}
}
